package com.mall.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.dao.UserMapper;
import com.mall.entity.User;
import com.mall.service.UserService;
import com.mall.vo.WebResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.Part;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserServicelmpl implements UserService {
    @Resource
    private JavaMailSender javaMailSender;
    @Autowired
    private UserMapper userMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public WebResult login(User user) {


        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //定义一个对象，方便接受参数
        User u = null;
        if (StringUtils.isEmpty(user.getEmail())&&StringUtils.isEmpty(user.getPassword())){
            return WebResult.builder().code(315).message("邮箱或密码为空").build();
        }

        //向数据库中查询
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",user.getEmail());
        u =userMapper.selectOne(queryWrapper);
        if (u==null){
            return WebResult.builder().code(317).message("用户不存在").build();
        }
        //MD5匹配,解密再碰撞
        if (!passwordEncoder.matches(user.getPassword(),u.getPassword())){
            return WebResult.builder().code(319).message("密码错误").build();
        }
        try {
            //设置token值
            String token = UUID.randomUUID().toString().replace("-", "");
            String s = JSON.toJSONString(u);
            stringRedisTemplate.opsForValue().set(token,s,30, TimeUnit.MINUTES);
            return WebResult.builder().code(200).message("登陆成功").data(token).build();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public WebResult sendcode(String email, String code) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("西柚商城项目注册验证码");
        String ncode = code.split("_")[0];
        simpleMailMessage.setText("尊敬的："+email+"您好！您的注册验证码为"+ncode+"有效期为十分钟");
        simpleMailMessage.setTo(email);
        simpleMailMessage.setFrom("2798524282@qq.com");
        javaMailSender.send(simpleMailMessage);
        return WebResult.builder().code(200).message("验证码发送成功").build();
    }

    @Override
    public WebResult editor(User user,long id) {
        try {
            userMapper.editoruser(user.getUsername(),user.getPhone(),user.getBankcards(),user.getRegion(),user.getSex(),user.getImg(),id);
            return WebResult.builder().code(200).message("用户信息编辑成功").build();
        }catch (Exception e){
            e.printStackTrace();
            return WebResult.builder().code(503).message("用户信息编辑错误").build();
        }
    }

    @Override
    public String uploadpic(Part image) {
        return null;
    }
}
